package com.vcourse.service.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vcourse.service.admin.entity.RolePermission;
import com.vcourse.service.admin.mapper.RolePermissionMapper;
import com.vcourse.service.admin.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色权限 服务实现类
 * </p>
 *
 * @author Zengsh123
 * @since 2020-06-15
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Override
    public void saveRolePermissionRelationShip(String roleId, String[] permissionId) {
        // 先删除该角色下的所有其他权限
        baseMapper.delete(new QueryWrapper<RolePermission>().eq("role_id", roleId));

        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (String s : permissionId) {
            if(StringUtils.isBlank(s)) {
                continue;
            }
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId).setPermissionId(s);
            rolePermissionList.add(rolePermission);
        }
        this.saveBatch(rolePermissionList);
    }
}
